<template>
	<view class="box">
		<form @submit="formSubmit">
			<view class="uni-form-item">
				<input name="pwd" type="password" placeholder="请输入原密码" />
			</view>
			<view class="uni-form-item">
				<input name="newPwd1" type="password" placeholder="请输入新密码" />
			</view>
			<view class="uni-form-item">
				<input name="newPwd2" type="password" placeholder="请确认新密码" />
			</view>
			<view class="btn-area">
				<button formType="submit" type="primary">确认修改</button>
			</view>
		</form>
	</view>
</template>

<script>
	var graceChecker = require("../../common/graceChecker.js");
	import {
		saveUserPasswd
	} from '../../common/api.js'
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			formSubmit(e) {
				//定义表单规则
				var rule = [
					{
						name: "pwd",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请输入原密码"
					},
					{
						name: "newPwd1",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请输入新密码"
					},
					{
						name: "newPwd2",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请确认新密码"
					}
				];
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if (checkRes) {
					let reqData = {
						user_token: this.$store.state.userInfo.user_token,
						old_user_password: formData.pwd,
						user_password: formData.newPwd1,
						user_com_passwd: formData.newPwd2
					}
					
					saveUserPasswd(reqData).then(res => {
						let resData = res.data
						if (resData.status === 'error') {
							uni.showToast({
								title: resData.msg,
								icon: "none"
							});
						} else {
							uni.showToast({
								title: '修改成功',
								icon: "none"
							})
							setTimeout(function(){
								uni.reLaunch({
								url: '../tabBar/my/my'
							})
							},1000)
							
						}

					}).catch(error => {
						console.log(error)
					})

				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				}
			},

		},
	}
</script>

<style>
	.box {
		padding: 40upx 30upx 0;
	}

	.uni-form-item {
		padding: 10upx 20upx;
		margin-bottom: 20upx;
		background-color: #F9F9F9;
	}

	.btn-area button {
		margin-bottom: 20upx;
	}
</style>
